package com.xdq.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xdq.servicebase.exceptionhandler.GuliException;
import com.xdq.serviceedu.entity.EduChapter;
import com.xdq.serviceedu.entity.EduVideo;
import com.xdq.serviceedu.entity.chapter.ChapterVo;
import com.xdq.serviceedu.entity.chapter.VideoVo;
import com.xdq.serviceedu.mapper.EduChapterMapper;
import com.xdq.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdq.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-09-14
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);
        List<ChapterVo> finalList = new ArrayList<>();
//        List<VideoVo> videoList = new ArrayList<>();
        eduChapterList.forEach(new Consumer<EduChapter>() {
            @Override
            public void accept(EduChapter eduChapter) {
                ChapterVo chapterVo = new ChapterVo();
                BeanUtils.copyProperties(eduChapter, chapterVo);
                finalList.add(chapterVo);

                List<VideoVo> videoList = new ArrayList<>();
                eduVideoList.forEach(new Consumer<EduVideo>() {
                    @Override
                    public void accept(EduVideo eduVideo) {
                        if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                            VideoVo videoVo = new VideoVo();
                            BeanUtils.copyProperties(eduVideo, videoVo);
                            videoList.add(videoVo);
                        }
                    }
                });
                chapterVo.setChildren(videoList);
            }
        });
        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) throws GuliException {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);

        if (count > 0) {
            throw new GuliException(20001, "不能删除");
        } else {
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
